package org.dromara.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.dromara.business.domain.LzshBusinessInfoDataTotal;
import org.dromara.business.domain.bo.LzshBusinessInfoBo;
import org.dromara.business.domain.vo.LzshBusinessInfoVo;
import org.dromara.business.domain.vo.LzshBusinessMenuVo;
import org.dromara.business.service.ILzshBusinessInfoService;
import org.dromara.business.service.ILzshBusinessMenuService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 比算信息
 *
 * @author sxl
 * @date 2024-08-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/businessInfo")
public class LzshBusinessInfoController extends BaseController {


    private final ILzshBusinessInfoService lzshBusinessInfoService;
    private final ILzshBusinessMenuService lzshBusinessMenuService;


    /**
     * 查询比算信息列表
     */
    @GetMapping("/api/list")
    public TableDataInfo<LzshBusinessInfoVo> listApi(LzshBusinessInfoBo bo, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        bo.setCreateBy(userId);
        return lzshBusinessInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询比算信息列表
     */
    @SaCheckPermission("business:businessInfo:list")
    @GetMapping("/list")
    public TableDataInfo<LzshBusinessInfoVo> list(LzshBusinessInfoBo bo, PageQuery pageQuery) {
        return lzshBusinessInfoService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取比算信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:businessInfo:query")
    @GetMapping("/{id}")
    public R<LzshBusinessInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(lzshBusinessInfoService.queryById(id));
    }

    /**
     * 新增比算信息,无权限
     */
    @Log(title = "比算信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/api")
    public R<LzshBusinessInfoVo> add(@Validated(AddGroup.class) @RequestBody LzshBusinessInfoBo bo) {
        Long userId = LoginHelper.getUserId();
        bo.setCreateBy(userId);
        LzshBusinessMenuVo lzshBusinessMenuVo = lzshBusinessMenuService.queryById(bo.getMenuId());
        bo.setMenuName(lzshBusinessMenuVo.getMenuName());
        return R.ok( lzshBusinessInfoService.insertByBo(bo));
    }


    /**
     * 新增比算信息,有权限
     */
    @SaCheckPermission("business:businessInfo:add")
    @Log(title = "比算信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<LzshBusinessInfoVo> add1(@Validated(AddGroup.class) @RequestBody LzshBusinessInfoBo bo) {
        return R.ok( lzshBusinessInfoService.insertByBo(bo));
    }


    /**
     * 修改比算信息
     */
    @SaCheckPermission("business:businessInfo:edit")
    @Log(title = "比算信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LzshBusinessInfoBo bo) {
        return toAjax(lzshBusinessInfoService.updateByBo(bo));
    }

    /**
     * 删除比算信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:businessInfo:remove")
    @Log(title = "比算信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(lzshBusinessInfoService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 按月份统计每个月的填写数量
     */
    @GetMapping("/api/monthNum")
    public R<Map<String,List<LzshBusinessInfoDataTotal.totalData>>>getMonthNum(@Param("year") String year){
        return R.ok(lzshBusinessInfoService.getMonthNum(year));
    }

    /**
     * 按城市统计填写数量
     */
    @GetMapping("/api/cityNum")
    public R<Map<String,List<LzshBusinessInfoDataTotal.totalData2>>>getCityNum(){
        return R.ok(lzshBusinessInfoService.getCityNum());
    }

}
